<template>
  <div class="login">
    <div class="logo_title">
      <img src="@/assets/logo/logo2.png" alt="" />
      <div class="desc">
        <p>可燃气体监测系统</p>
        <p style="font-size: 11px; font-weight: 500">
          <!-- KJ70X SUPERVISION SYSTEM OF COAL MINE SAFETY -->
          Combustible gas monitoring system
        </p>
      </div>
    </div>
    <div class="login_top">
      <el-row :gutter="20" style="display: flex; align-items: center">
        <el-col :span="16">
          <img src="@/assets/images/背景3.png" alt="" class="bg" />
        </el-col>
        <el-col :span="8">
          <el-form
            ref="loginRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <h3 class="title">
              <img src="@/assets/logo/logo2.png" alt="" /><span>欢迎登录</span>
            </h3>
            <p class="p_tabType">
              <span
                v-for="item in tabList"
                :key="item.code"
                @click="tabClick(item.code)"
                :class="tabType == item.code ? 'tabType active' : 'tabType'"
                >{{ item.name }}</span
              >
            </p>
            <div v-if="tabType == 1">
              <el-form-item prop="username">
                <el-input
                  v-model="loginForm.username"
                  type="text"
                  size="large"
                  auto-complete="off"
                  placeholder="请输入账号"
                >
                  <template #prefix
                    ><svg-icon
                      icon-class="user"
                      class="el-input__icon input-icon"
                  /></template>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  size="large"
                  auto-complete="off"
                  placeholder="请输入密码"
                  @keyup.enter="handleLogin"
                >
                  <template #prefix
                    ><svg-icon
                      icon-class="password"
                      class="el-input__icon input-icon"
                  /></template>
                </el-input>
              </el-form-item>
            </div>
            <div v-if="tabType == 2" style="text-align: center">
              <img
                style="width: 260px; height: 260px; margin: 0 auto"
                src="@/assets/images/code.png"
                alt=""
              />
            </div>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="loginForm.code"
                size="large"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter="handleLogin"
              >
                <template #prefix
                  ><svg-icon
                    icon-class="validCode"
                    class="el-input__icon input-icon"
                /></template>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-checkbox
              v-model="loginForm.rememberMe"
              style="margin: 0px 0px 25px 0px; font-size: 20px"
              >记住账号</el-checkbox
            >
            <el-form-item style="width: 100%">
              <el-button
                :loading="loading"
                size="large"
                type="primary"
                style="
                  width: 100%;
                  height: 60px;
                  background-color: rgba(64, 158, 255, 1);
                  border: none;
                  border-radius: 8px;
                  font-size: 20px;
                "
                @click.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right">
                <router-link class="link-type" :to="'/register'"
                  >立即注册</router-link
                >
              </div>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <div>Copyright © 1998-2024 SANHENGKEJI 版权所有</div>
      <div class="footer1">
        <div>服务电话：0519-85129371</div>
        <div>江苏三恒科技股份有限公司-技术支持</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const tabList = reactive([
  //   {
  //   code:1,
  //   name:'账号密码登录'
  // }
  // ,{
  //   code:2,
  //   name:'二维码登录'
  // }
]);
const tabType = ref(1);

const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: "",
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(false);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          const query = route.query;
          const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
            if (cur !== "redirect") {
              acc[cur] = query[cur];
            }
            return acc;
          }, {});
          router.push({
            path: redirect.value || "/shouye/lbsy",
            query: otherQueryParams,
          });
        })
        .catch(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

function getCode() {
  getCodeImg()
    .then((res) => {
      captchaEnabled.value =
        res.captchaEnabled === undefined ? true : res.captchaEnabled;
      if (captchaEnabled.value) {
        codeUrl.value = "data:image/gif;base64," + res.img;
        loginForm.value.uuid = res.uuid;
      }
    })
    .catch((res) => {
      captchaEnabled.value = false;
    });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}
function tabClick(code) {
  tabType.value = code;
  console.log(code, tabType.value, "打击打击");
}

getCode();
getCookie();
</script>

<style lang='scss' scoped>
.logo_title {
  display: flex;
  align-items: center;
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 999;
  p {
    padding: 0;
    margin: 0;
    font-size: 24px;
  }
  img {
    width: 30px;
    height: 30px;
  }
  .desc {
    font-family: "思源黑体 Medium";
    color: rgba(34, 68, 119, 1);
    font-size: 24px;
    margin-left: 10px;
    font-weight: 700;
  }
}
:deep(.el-input__wrapper) {
  box-shadow: none !important;
}
.p_tabType {
  text-align: center;
  font-size: 18px;
  font-weight: 600;
}

.tabType {
  color: rgba(34, 68, 119, 0.4);
  cursor: pointer;
  margin: 0 10px;
  &:nth-child(1) {
    position: relative;
    &::after {
      position: absolute;
      content: "";
      top: 50%;
      right: -10px;
      transform: translateY(-50%);
      width: 1px;
      height: 18px;
      background-color: rgba(34, 68, 119, 0.08);
    }
  }
  &.active {
    color: rgba(64, 158, 255, 1);
  }
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  background: radial-gradient(
    22.89% 50.3% at 50.0115713955103% 49.6987951807229%,
    rgba(243, 246, 252, 1) 0%,
    rgba(234, 240, 253, 1) 100%
  );
  height: 100%;
  overflow: hidden;
  position: relative;
  .login_top {
    // position: absolute;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
.bg {
  background-image: none;
  display: block;
  max-width: 100%;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: rgba(34, 68, 119, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  span {
    font-size: 40px;
    font-weight: 700;
    margin-left: 10px;
  }
}

.login-form {
  position: absolute;
  top: 20%;
  right: -5%;
  // font-family: "思源黑体 Medium";
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.4);
  border: 1px solid rgba(64, 158, 255, 0.16);
  width: 457px !important;
  height: 548px;
  padding: 50px 25px 50px 25px;
  margin-right: 10%;
  .el-input {
    height: 60px;
    font-size: 20px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  padding: 0 35px;
  height: 40px;
  position: absolute;
  bottom: 10px;
  width: 100%;
  text-align: center;
  color: #a0b0cb;
  font-family: Arial;
  font-size: 14px;
  letter-spacing: 1px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .footer1 {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-between;
  }
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
